package com.example.text51;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class MainActivity extends AppCompatActivity {

    String UserName, Password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        TextView name = findViewById(R.id.UserName);
        TextView password = findViewById(R.id.password);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserName =  name.getText().toString();
                Password =  password.getText().toString();
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            URL url = new URL("https://www.wanandroid.com/user/login");
                            HttpURLConnection coon = (HttpURLConnection) url.openConnection();
                            coon.setRequestMethod("POST");
                            coon.setDoOutput(true);
                            OutputStream outputStream = coon.getOutputStream();
                            String data = "username=" + UserName + "&password=" + Password;
                            outputStream.write(data.getBytes(StandardCharsets.UTF_8));
                            if(coon.getResponseCode()==200){
                                InputStream input = coon.getInputStream();
                                InputStreamReader reader = new InputStreamReader(input);
                                BufferedReader br = new BufferedReader(reader);
                                StringBuffer sbf = new StringBuffer();
                                String str = null;
                                while ((str = br.readLine())!=null){
                                    sbf.append(str);
                                }
                                str = sbf.toString();
                                System.out.println(str);
                                JSONObject jsonObject = new JSONObject(str);
                                int s = jsonObject.getInt("errorCode");
                                if (s==0){
                                    System.out.println("登录成功");
                                }
                            }


                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }.start();
            }
        });

    }
}